//
//  EnemyUnit.m
//  ProjectLila
//
//  Created by Tedo on 6/19/10.
//  Copyright 2010 sushigames. All rights reserved.
//

#import "EnemyUnit.h"

@implementation EnemyUnit

static NSMutableArray * allEnemyUnits = nil;

+(NSMutableArray *) allUnits {
    @synchronized(allEnemyUnits) {
        if (allEnemyUnits == nil)
            allEnemyUnits = [[NSMutableArray alloc] init];
        return allEnemyUnits;
    }
	return nil;
}

+(NSMutableArray *) allUnitsNotDead {
	NSMutableArray *array = [[NSMutableArray alloc] init];
	for (EnemyUnit *unit in [EnemyUnit allUnits]) {
		if (unit.state != DEAD) {
			[array addObject:unit];
		}
	}
	return array;
}

+(void)track: (EnemyUnit *)aUnit {
    @synchronized(allEnemyUnits) {
        [[EnemyUnit allUnits] addObject:aUnit];
		NSLog( @"EnemyUnit Child Count: %d", [[EnemyUnit allUnits] count] );
    }
}

+(void)untrack: (EnemyUnit *)aUnit {
    @synchronized(allEnemyUnits) {
        [[EnemyUnit allUnits] removeObject:aUnit];
    }
}

+(void)clearAllUnits {
	@synchronized(allEnemyUnits) {
		[[EnemyUnit allUnits] removeAllObjects];
	}
}

+(id)unitFromId: (NSString *)identifier {
	EnemyUnit *unit = (EnemyUnit *)[Unit unitFromId:identifier];
	[EnemyUnit track:unit];
	return unit;
}

-(id)init {
    self = [super init];
    if (self) {
		[EnemyUnit track:self];
	}
    return self;
}

-(void)dealloc {
	NSLog( @"EnemyUnit dealloc" );
	
    [EnemyUnit untrack:self];
    [super dealloc];
}

@end
